﻿using JESAI.Core.Modularity;
using System.ComponentModel;
using System.Configuration;
using Sys.Net.Http;
using Sys.Workflow.Engine.Impl.Bpmn.Webservice;
using Sys.Workflow.Services.Rest;
using Sys.Workflow;
using Microsoft.OpenApi.Models;

namespace JESAI.Platform.WebApi.Modules
{
    /// <summary>
    /// AspNetCore模块
    /// </summary>
    [Description("WorkFlow模块")]
    public class WorkFlowModule:JESAIModule
    {
        /// <summary>
        /// 模块级别。级别越小越先启动
        /// </summary>
        public override ModuleLevel Level => ModuleLevel.WorkFlow;

        /// <summary>
        /// 模块启动顺序。模块启动的顺序先按级别启动，同一级别内部再按此顺序启动，
        /// </summary>
        public override int Order => 2;

        /// <summary>
        /// 添加服务。将模块服务添加到依赖注入服务容器中
        /// </summary>
        /// <param name="services">服务集合</param>
        public override IServiceCollection AddServices(IServiceCollection services)
        {          
            services.AddControllers().AddProcessEngineRestServices(services.GetConfiguration());
            //services.AddHttpContextAccessor();
            services.AddProcessEngine(services.GetConfiguration());
            //services.AddEndpointsApiExplorer();
            return services;
        }

        public override void UseModule(IApplicationBuilder app)
        {
            
            var lifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
            app.UseProcessEngine(lifetime);
            app.UseWorkflow();
            //app.UseEndpoints(endpoints =>
            //{
            //    endpoints.MapControllers();
            //});
        }
    }
}
